package controllers
{
	import actions.AddAction;
	import actions.DeleteAction;
	import actions.MoveAction;
	import actions.ResizeAction;
	
	import events.ObjectEvent;
	
	import model.CircleObject;
	import model.ObjectType;
	import model.RectangleObject;
	import model.WBObject;
	import model.WhiteBoardModel;
	
	import mx.collections.ArrayCollection;
	import mx.utils.object_proxy;
	
	import sync.WhiteBoardSynchronizer;

	public class WhiteBoardController
	{
		private var _model:WhiteBoardModel;
		private var _view:WhiteBoardView;
		private var _synchronizer:WhiteBoardSynchronizer;
		
		public function WhiteBoardController()
		{
			_model = new WhiteBoardModel();
			
			_view = new WhiteBoardView();
			_view.addEventListener(ObjectEvent.OBJECT_ADDED, onObjectAdded, false, 0, true);
			_view.addEventListener(ObjectEvent.OBJECT_MOVED, onObjectMoved, false, 0, true);
			_view.addEventListener(ObjectEvent.OBJECT_DELETED, onObjectDeleted, false, 0, true);
			_view.addEventListener(ObjectEvent.OBJECT_RESIZED, onObjectResized, false, 0, true);
			
			_synchronizer = new WhiteBoardSynchronizer();
			_synchronizer.model = model;
			_synchronizer.view = view;
		}
		
		public function get synchronizer():WhiteBoardSynchronizer
		{
			return _synchronizer;
		}

		public function set synchronizer(value:WhiteBoardSynchronizer):void
		{
			_synchronizer = value;
		}

		public function get view():WhiteBoardView
		{
			return _view;
		}

		public function set view(value:WhiteBoardView):void
		{
			_view = value;
		}

		public function get model():WhiteBoardModel
		{
			return _model;
		}

		public function set model(value:WhiteBoardModel):void
		{
			_model = value;
		}
		
		
		private function onObjectAdded(event:ObjectEvent):void
		{
			//create action
			var action:AddAction = new AddAction();
			action.xPos = event.objectXPos;
			action.yPos = event.objectYPos;
			action.typeOfObject = event.objectType;
			
			_synchronizer.addAction(action);
		}
		
		private function onObjectMoved(event:ObjectEvent):void
		{
			//create action
			var action:MoveAction = new MoveAction();
			action.deltaX = event.deltaX;
			action.deltaY = event.deltaY;
			action.uid = event.object.uid;
			
			_synchronizer.addAction(action);
		}
		
		private function onObjectDeleted(event:ObjectEvent):void
		{
			var action:DeleteAction = new DeleteAction();
			action.uid = event.object.uid;
			
			_synchronizer.addAction(action);
		}
		
		private function onObjectResized(event:ObjectEvent):void
		{
			var action:ResizeAction = new ResizeAction();
			action.uid = event.object.uid;
			action.deltaW = event.deltaX;
			action.deltaH = event.deltaY;
			
			_synchronizer.addAction(action);
		}
	}
}